<?php
namespace api\tv\controller;

use api\tv\controller\Base;
use think\Config;
use think\Request;
use app\common\App;
use core\tv\model\VideoCateModel;
use core\tv\model\VideoModel;
use core\tv\model\VideoSourceModel;
use core\tv\model\StoreGroupModel;
class Video extends Base
{

    /**
     * 循环视频列表
     */
    public function loop(Request $request)
    {
        $group_id = $request->param('group_id');
        $map['status'] = 1;
        $map['video_type'] = 0;
        $list = VideoModel::getInstance()->order('sort DESC')->where($map)->select();
        if($list){
            $videos = StoreGroupModel::getInstance()->where('id',$group_id)->value('videos');
            if($videos){
                $videos = explode(',',$videos);
                foreach ($list as $key => &$item) {
                    if(!in_array($item['id'],$videos)){
                        unset($list[$key]);
                    }
                    unset($item);
                }
                $this->api(200,'获取视频列表成功',$list);
            }
        }
        $this->api(400,'获取视频列表失败');
    }

    /**
     * 获取视频资源信息
     */
    public function info(Request $request)
    {
        $video_id = $request->param('video_id');
        //$video_id = 2;

        if(!$video_id){
            $this->api(400,'缺少视频ID');
        }
        $map['video_id'] = $video_id;
        $map['status'] = 1;
        $list = VideoSourceModel::getInstance()->where($map)->order('play_sort ASC')->paginate(8);
        if ($list) {
            $this->api(200,'获取视频资源信息成功',$list);
        }else{
            $this->api(400,'获取视频资源信息失败');
        }
    }

    /**
     * 获取指定分类下的视频
     */
    public function list(Request $request)
    {
        $cate_id = $request->param('cate_id');
        if(!$cate_id){
            $this->api(400,'缺少分类ID');
        }
        $map['status'] = 1;
        $map['cate_id'] = $cate_id;
        $cate = VideoCateModel::getInstance()->find($cate_id);
        if(!$cate){
            $this->api(400,'分类不存在');
        }
        $list = VideoModel::getInstance()->order('sort DESC')->where($map)->paginate(8);
        if ($list) {
            $data = [
                'list' => $list,
                'cate' => $cate
            ];
            $this->api(200,'获取分类视频列表成功',$data);
        }else{
            $this->api(400,'获取分类视频列表失败');
        }
    }

    //获取视频信息
    public function one(Request $request)
    {
        $video_id = $request->param('video_id');
        if(!$video_id){
            $this->api(400,'缺少视频ID');
        }
        $data = VideoModel::getInstance()->find();
        if($data){
            $this->api(200,'获取视频信息成功',$data);
        }else{
            $this->api(400,'获取视频信息失败');
        }
    }

    /**
     * 视频分类
     */
    public function cate(Request $request)
    {
        $map['status'] = 1;
        $cates = VideoCateModel::getInstance()->where($map)->order('sort DESC')->select();
        $this->api(200,'获取视频分类成功',$cates);
    }

    /**
     * 获取图片
     */
    public function pic()
    {
      	$data = [
          'load' => Config::get('loading_pic'),
          'home' => Config::get('home_pic'),
          'fine' => Config::get('fine_pic')
        ];
        $this->api(200,'获取图片成功',$data);
    }

    /**
     * 获取滚动公告
     */
    public function notice()
    {
        $notices = Config::get('notice_list');
        $this->api(200,'获取滚动公告成功',$notices);
    }
}
 